package org.molamil.acto.calls {

	import org.molamil.acto.core.session.ActoSession;
	import org.molamil.acto.core.loggers.Logger;
	import org.molamil.acto.props.NativePropsParser;
	import org.molamil.acto.utils.StringUtils;
	
	/**
	 * XXX: Document.
	 * 
	 * @author Jorge Hernandez
	 */
	public class CallsParser {
		
		
		// CONSTANTS
		
		private static const log:Logger = Logger.getLogger("CallsParser");
		
		
		// PROPERTIES
		
		protected var xml:XML;
		protected var session:ActoSession;
		
		
		// CONSTRUCTOR
		
		public function CallsParser(session:ActoSession = null) {
			this.session = session != null ? session : new ActoSession();
		}
		
		
		// PUBLIC METHODS
		
		public function parse(xml:XML):Array {
			
			this.xml = xml;
			
			// Create the object.
			var callContexts:Array = new Array();
			
			// Get params.
			var callNodes:XMLList = xml.call;
			log.debug(callNodes.length() + " calls found");
			
			// Iterate the calls.
			for each (var callNode:XML in callNodes) {
				
				var call:CallContext = new CallContext();
			
				var method:String = StringUtils.trim(callNode.attribute("method"));
				if (method != "")
					call.method = method;
				
				//TODO: Make propsParser configurable.
				var propsParser:NativePropsParser = new NativePropsParser(session);
				call.args = propsParser.getArrayDescriptors(callNode.item).value;
				
				callContexts.push(call);
			
			}
			
			return callContexts;
			
		}
		
		
	}
	
}
